//
//  CycleHorButton.m
//  cycleControlExtension
//
//  Created by 苹果笔记本 on 2020/11/25.
//  Copyright © 2020 cycle. All rights reserved.
//

#import "CycleHorButton.h"
#import "UIView+cyFrame.h"
@interface CycleHorButton()

@end
@implementation CycleHorButton


- (void)setup{
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.btnW = 14;
    self.btnH = self.btnW;
    self.btnMargin = 8;
    [self addObserver:self forKeyPath:@"btnW" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"btnH" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"btnMargin" options:NSKeyValueObservingOptionNew context:nil];

}
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
    if (keyPath.length) {
        if ([keyPath isEqualToString:@"btnW"]) {
           [self layout];
        }
        if ([keyPath isEqualToString:@"btnH"]) {
            [self layout];
        }
        if ([keyPath isEqualToString:@"btnMargin"]) {
            [self layout];
        }
    }
}
/**
 *  为了代码的通用性,写下初始化方法，以后无论是纯代码还是xib都可以调用
 */
-(instancetype)initWithFrame:(CGRect)frame{
    if([super initWithFrame:frame]){
        [self setup];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self layout];
}
///layout
- (void)layout{
    //1. 图片
    self.imageView.cy_x = 0;
    self.imageView.cy_width = self.btnW;
    self.imageView.cy_height = self.btnH;
    self.imageView.cy_y = (self.cy_height - self.imageView.cy_width)*0.5;
    
    
    //2. 文字
    self.titleLabel.cy_x = CGRectGetMaxX(self.imageView.frame)+self.btnMargin;
    self.titleLabel.cy_y = 0;
    self.titleLabel.cy_width = self.cy_width-self.titleLabel.cy_x;
    self.titleLabel.cy_height = self.cy_height;
}

@end
